import type { App } from "vue";
import HomeLayout from "@/layouts/HomeLayout.vue";
import ParentLayout from "@/layouts/ParentLayout.vue";
import type { RouteRecordRaw } from "vue-router";
import { createRouter, createWebHistory } from "vue-router";
import { createRouterGuards } from "./router-guards";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/base/home",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login/index.vue"),
    meta: {
      menuKey: "login",
    },
  },
  {
    path: "/base",
    component: () => import("@/layouts/BaseLayout.vue"),
    children: [
      {
        path: "",
        component: HomeLayout,
        children: [
          {
            path: "home",
            name: "home",
            component: () => import("@/views/Home/index.vue"),
            meta: {
              title: "主页",
              menuKey: "home",
            },
          },
          {
            path: "appList",
            name: "appList",
            component: () => import("@/views/App/AppList/index.vue"),
            meta: {
              title: "全部应用",
              menuKey: "app",
            },
          },
          {
            path: "my",
            name: "my",
            component: () => import("@/views/My/index.vue"),
            meta: {
              title: "我的",
              menuKey: "my",
            },
          },
        ],
      },
      {
        path: "",
        component: ParentLayout,
        children: [
          {
            name: "accountSettings",
            path: "accountSettings",
            component: () => import("@/views/AccountSettings/index.vue"),
            meta: {
              title: "账户设置",
            },
          },
          {
            name: "changePhone",
            path: "changePhone",
            component: () =>
              import("@/views/AccountSettings/components/ChangePhone.vue"),
            meta: {
              title: "更换手机号",
            },
          },
          {
            name: "changeEmail",
            path: "changeEmail",
            component: () =>
              import("@/views/AccountSettings/components/ChangeEmail.vue"),
            meta: {
              title: "更换邮箱",
            },
          },
          {
            name: "userInfo",
            path: "userInfo",
            component: () => import("@/views/PersonalInfo/index.vue"),
            meta: {
              title: "个人信息",
            },
          },
          {
            name: "changeName",
            path: "changeName",
            component: () =>
              import("@/views/PersonalInfo/components/ChangeName.vue"),
            meta: {
              title: "更改姓名",
            },
          },
          {
            path: "appDetail/:appId",
            name: "appDetail",
            component: () => import("@/views/App/AppDetail/index.vue"),
            meta: {
              title: "应用详情",
            },
          },
          {
            path: "appFill",
            name: "fillForm",
            component: () => import("@/views/App/FillForm/index.vue"),
            meta: {
              title: "填写表单",
            },
          },
        ],
      },
    ],
  },
];

const router = createRouter({
  // process.env.BASE_URL
  history: createWebHistory(),
  routes,
});

export function setupRouter(app: App) {
  app.use(router);
  // 创建路由守卫
  createRouterGuards();
}

export default router;
